Page({
  data: {
    dataList: [], // 修改了这里
  },
  onLoad: function () {
    this.loadDatas();
  },
  onShow: function () {
    this.loadDatas();
  },
  loadDatas: function () {
    const dataList = wx.getStorageSync('dataList') || []; // 修改了这里
    this.setData({ dataList }); // 修改了这里
  },
  addData: function () {
    wx.navigateTo({
      url: '/pages/add/index',
    });
  },
  viewData: function (e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/edit/index?id=${id}`,
    });
  },
  deleteData: function(e) {
    const id = e.currentTarget.dataset.id;
    const dataList = this.data.dataList.filter(Data => Data.id !== id); // 修改了这里
    this.setData({ dataList }); // 修改了这里
    wx.setStorageSync('dataList', dataList); // 更新本地存储并修改了这里
    wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 2000
    });
  },
  toggleCompletion: function(e) {
    const index = e.currentTarget.dataset.index;
    // 直接修改数组中对应项的 completed 状态
    const newTodos = this.data.dataList.map((todo, i) => {
      if (i === index) {
        return {...todo, completed: !todo.completed};
      }
      return todo;
    });
    // 更新页面数据
    this.setData({ dataList: newTodos });
    // 保存更新后的待办事项列表到本地存储
    wx.setStorageSync('dataList', newTodos);
  },
  testCheckboxChange: function(e) {
    debugger
    console.log('Test checkbox changed');
  }
});